package com.chuji.crmcj.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuji.crmcj.R;
import com.chuji.crmcj.act.MagWorkFormActivity;
import com.chuji.crmcj.act.MainActivity;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.CalendarView;
import com.chuji.crmcj.view.CalendarViewDialog;

import java.util.Date;


public class ShopFormFragment extends Fragment implements View.OnClickListener{
    //下拉框
    private com.chuji.crmcj.view.NiceSpinner mSp_form_type;
    private com.chuji.crmcj.view.NiceSpinner mSp_form_name;
    //日历图片
    private ImageView mIv_calendar;
    //日期
    private TextView mTv_data;
    WindowManager windowManager;
    CalendarViewDialog dialog;
    WindowManager.LayoutParams lp;
    //数据
    String [] formNames={"选择报表名称","进店客流量","电话客流量","首次客流量","战败率"};
    String [] formTypes={"选择报表类型","日报","月报","年报"};
    //适配器
    ArrayAdapter<String> foemNameAdapter;
    ArrayAdapter<String> foemTypeAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_shop_form, container, false);
        //初始化组件
        mSp_form_type = (com.chuji.crmcj.view.NiceSpinner) view.findViewById(R.id.sp_form_type);
        mSp_form_name = (com.chuji.crmcj.view.NiceSpinner) view.findViewById(R.id.sp_form_name);
        mIv_calendar = (ImageView) view.findViewById(R.id.iv_calendar);
        mTv_data = (TextView) view.findViewById(R.id.tv_data);
        //设置点击
        mIv_calendar.setOnClickListener(this);
        //创建adapter对象
        foemNameAdapter=new ArrayAdapter<String>(UIUtils.getContext(),android.R.layout.simple_spinner_item,formNames);
        foemTypeAdapter=new ArrayAdapter<String>(UIUtils.getContext(),android.R.layout.simple_spinner_item,formTypes);
        //填充
        mSp_form_name.setAdapter(foemNameAdapter);
        mSp_form_type.setAdapter(foemTypeAdapter);
        mSp_form_type.setSelected(false);
        mSp_form_name.setSelected(false);
//            添加itemSeleted事件
        mSp_form_name.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
                                                {
                                              @Override
                                              public void onItemSelected(AdapterView<?> parent, View view,
                                                                         int position, long id) {
                                                  UIUtils.showToastSafe(formNames[position]);
                                              }

                                              /**
                                               * 没有选中。。自动调用
                                               */
                                              @Override
                                              public void onNothingSelected(AdapterView<?> parent) {
                                                  // TODO Auto-generated method stub

                                              }
                                          }

        );
        mSp_form_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
                                                {
                                                    @Override
                                                    public void onItemSelected(AdapterView<?> parent, View view,
                                                                               int position, long id) {
                                                        UIUtils.showToastSafe(formTypes[position]);
                                                    }

                                                    /**
                                                     * 没有选中。。自动调用
                                                     */
                                                    @Override
                                                    public void onNothingSelected(AdapterView<?> parent) {
                                                        // TODO Auto-generated method stub

                                                    }
                                                }

        );
        return view;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_calendar:
                dialog = new CalendarViewDialog(getContext());
                dialog.show();
                dialog.calendar.setOnItemClickListener(new CalendarView.OnItemClickListener() {
                    @Override
                    public void OnItemClick(Date selectedStartDate, Date selectedEndDate, Date downDate) {
                        if (dialog.calendar.isSelectMore()) {
                            UIUtils.showToastSafe(dialog.format.format(selectedStartDate) + "到" + dialog.format.format(selectedEndDate));
                        } else {
                            mTv_data.setText(dialog.format.format(downDate));
                            dialog.dismiss();
                        }
                    }
                });
                windowManager =getActivity().getWindowManager();
                lp = dialog.getWindow().getAttributes();  //获取对话框当前的参数值
                //p.height = (int) (d.getHeight() * 0.3);   //高度设置为屏幕的0.3
                lp.width = windowManager.getDefaultDisplay().getWidth();//为获取屏幕宽、高,宽度设置为全屏
                dialog.getWindow().setAttributes(lp);     //设置生效
        }
    }
}
